/**
 *
 */
package com.ssyt.examinationsystem.dao.impl;

import com.jdbc.util.JdbcUtils;
import com.ssyt.examinationsystem.dao.ITeacherInfoDAO;
import com.ssyt.examinationsystem.entity.TeacherInfo;
import com.ssyt.examinationsystem.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * 教师的数据层的实体类
 *
 * @author Administrator
 */
public class TeacherInfoDAOImpl implements ITeacherInfoDAO {

    /**
     * 管理员和教师登录的方法
     */
    public boolean login(TeacherInfo teacher) {
        String sql = "select 1 from teacherinfo where Account=? and password=? and roleId=?";
        Object[] parameters = {teacher.getAccount(), teacher.getPassword(), teacher.getRole().getRoleId()};
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, parameters);
        return CollectionUtils.isEmptyForList(list);
    }

    @Override
    public List<Map<String, Object>> getTeacherInfos() {
        String sql = "select * from teacherinfo ";
        return JdbcUtils.executeQuery(sql);
    }

    @Override
    public int deleteTeacherInfo(String teaId) {
        String sql = "delete from teacherinfo where teaId=?";
        return JdbcUtils.executeUpdate(sql, teaId);
    }

    @Override
    public int addTeacherInfo(TeacherInfo tea) {
        String sql = "insert into teacherinfo values(null,?,?,?,?)";
        Object[] parameters = {tea.getTeaName(), tea.getAccount(), tea.getPassword(), tea.getRole().getRoleId()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    @Override
    public int updateTeacherInfo(TeacherInfo tea) {
        String sql = "update teacherinfo set teaname=?,account=?,password=?,roleid=? where teaid=?";
        Object[] parameters = {tea.getTeaName(), tea.getAccount(), tea.getPassword(), tea.getRole().getRoleId(),
                tea.getTeaId()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    @Override
    public Map<String, Object> getTeaByStuId(String teaId) {
        String sql = "select * from teacherinfo where teaid=?";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, teaId);
        return CollectionUtils.isEmptyForList(list) ? list.get(0) : null;
    }

}
